package com.fjwt.gz.service.activity.other.acte026;

import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.entity.ActivityAwardEntity;
import com.fjwt.gz.db.entity.ActivityEntity;
import com.fjwt.gz.db.entity.SysExportTaskEntity;
import com.fjwt.gz.db.vo.ExportAwardVO;
import com.fjwt.gz.service.activity.AbstractExportReport;
import com.fjwt.gz.service.impl.ActivityAwardService;
import com.fjwt.gz.service.impl.FansAwardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ------ 026商家券领取导出报表
 */
@Slf4j
@Service
public class ACTE026ExportReport extends AbstractExportReport<ExportAwardVO> {

    @Autowired
    private ActivityAwardService activityAwardService;
    @Autowired
    private FansAwardService fansAwardService;


    private static final Map<String, String> headerAliasMap = new HashMap<>();
    static {
        headerAliasMap.put("fansId", "用户唯一ID");
        headerAliasMap.put("realName", "参与客户姓名");
        headerAliasMap.put("phone", "参与客户手机号");
        headerAliasMap.put("isAuth", "参与客户是否实名");
        headerAliasMap.put("couponName", "领用卡券名称");
        headerAliasMap.put("couponPrice", "卡券金额");
        headerAliasMap.put("couponThirdId", "微信唯一卡券编号");
        headerAliasMap.put("createdAt", "参与时间");
        headerAliasMap.put("writeTime", "核销时间");
        headerAliasMap.put("status", "卡券状态");
        headerAliasMap.put("mchNo", "发放商户号");
        headerAliasMap.put("mchName", "发放商户名称");
    }


    @Override
    public String exportActivityExcel(SysExportTaskEntity sysExportTaskEntity) {
        ActivityEntity activityEntity = getRequestParam(sysExportTaskEntity.getRequestParam(), ActivityEntity.class);
        // 查询活动卡券
        List<ActivityAwardEntity> activityAwardList = activityAwardService.list(ActivityAwardEntity.gw()
                .eq(ActivityAwardEntity::getActivityId, activityEntity.getActivityId())
                .eq(ActivityAwardEntity::getAwardType, Constants.AWARD.COUPON));
        if (activityAwardList.isEmpty()) {
            throw new BizException("该活动未配置奖品！");
        }
        List<Long> awardIds = activityAwardList.stream().map(ActivityAwardEntity::getAwardId).toList();
        List<ExportAwardVO> list = fansAwardService.exportACTE026CouponAward(awardIds, activityEntity.getAgentNo());
        return createExcelAndUploadOssUrl(list, headerAliasMap, sysExportTaskEntity);
    }
}
